package com.wb.adutils.ads.gmad

import android.app.Activity
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import com.bytedance.msdk.adapter.gdt.GdtNetworkRequestInfo
import com.bytedance.msdk.adapter.pangle.PangleNetworkRequestInfo
import com.bytedance.msdk.api.AdError
import com.bytedance.msdk.api.v2.ad.splash.GMSplashAd
import com.bytedance.msdk.api.v2.ad.splash.GMSplashAdListener
import com.bytedance.msdk.api.v2.ad.splash.GMSplashAdLoadCallback
import com.bytedance.msdk.api.v2.slot.GMAdSlotSplash
import com.wb.adutils.AdBaseLoader
import com.wb.adutils.AdUtils
import com.wb.adutils.db.AdEntity
import com.wb.adutils.tools.AdConstants
import com.wb.adutils.tools.LogC


class GMSplashUtils(val adEntity: AdEntity?): AdBaseLoader(adEntity) {

    private var mTTSplashAd: GMSplashAd? = null

    override fun excLoadAd(activity: Activity) {
        try {
            isAdLoadSuccess = false
            val adSlot = GMAdSlotSplash.Builder()
                .setBidNotify(true)
                .setSplashPreLoad(true) //开屏gdt开屏广告预加载
                .setMuted(true) //声音开启
                .setTimeOut(timeout) //设置超时
                .setImageAdSize(width, height)
                .setSplashShakeButton(true) //合规设置，点击区域设置
                .build()
            mTTSplashAd = GMSplashAd(activity, adEntity?.adUnit)
            //自定义兜底方案 选择使用
            //穿山甲兜底，参数分别是appId和adn代码位。注意第二个参数是代码位，而不是广告位。
            val networkRequestInfoId = AdUtils.getSplashAdUnit()
            val gmNetworkRequestInfo = if (!TextUtils.isEmpty(networkRequestInfoId)) {
                if (AdUtils.getAdType() == AdUtils.TYPE_PANGLE) {
                    PangleNetworkRequestInfo(AdUtils.getAppId(), networkRequestInfoId)
                } else {
                    GdtNetworkRequestInfo(AdUtils.getAppId(), networkRequestInfoId)
                }
            } else null
            // gdt兜底
            // gmNetworkRequestInfo = new GdtNetworkRequestInfo("1101152570", "8863364436303842593");
            mTTSplashAd?.loadAd(adSlot, gmNetworkRequestInfo, object : GMSplashAdLoadCallback {
                override fun onSplashAdLoadFail(adError: AdError) {
                    LogC.e("开屏 -> onSplashAdLoadFail..." + adError.message)
                    excAdLoadFail(adError.code)
                }

                override fun onSplashAdLoadSuccess() {
                    if (timeoutDisposable?.isDisposed == true) { // 已超时
                        LogC.e("开屏 -> onSplashAdLoadSuccess...已超时")
                        return
                    }
                    LogC.e("开屏 -> onSplashAdLoadSuccess...")
                    isAdLoadSuccess = true
                    timeoutDisposable?.dispose()
                    listener?.onAdLoaded(null)
                }

                override fun onAdLoadTimeout() {
                    LogC.e("开屏 -> onAdLoadTimeout..." )
                    excAdLoadFail(AdConstants.AD_ERROR_CODE_TIMEOUT)
                }
            })
            LogC.e("开屏 -> 准备加载广告...")
        } catch (e: Exception) {
            e.printStackTrace()
            timeoutDisposable?.dispose()
            listener?.onAdLoadFail(AdConstants.AD_ERROR_CODE_EXCEPTION)
            release()
        }
    }


    override fun showAd(adRoot: ViewGroup?) {
        try {
            LogC.e("开屏 -> showAd...")
            if (adRoot == null) {
                release()
                return
            }
            if (isAdLoadSuccess && mTTSplashAd?.isReady == true) {
                mTTSplashAd?.setAdSplashListener(object : GMSplashAdListener {
                    override fun onAdClicked() {
                        LogC.e("开屏 -> showAd...onAdClicked...")
                        listener?.onAdClicked()
                    }

                    override fun onAdShow() {
                        LogC.e("开屏 -> showAd...onAdShow...")
                        listener?.onAdDisplay()
                    }

                    override fun onAdShowFail(adError: AdError) {
                        LogC.e("开屏 -> showAd...onAdShowFail..." + adError.message)
                        listener?.onAdLoadFail(adError.code)
                        release()
                    }

                    override fun onAdSkip() {
                        LogC.e("开屏 -> showAd...onAdSkip...")
                        listener?.onAdComplete()
                        release()
                    }

                    override fun onAdDismiss() {
                        LogC.e("开屏 -> showAd...onAdDismiss...")
                        listener?.onAdComplete()
                        release()
                    }

                })

                adRoot.removeAllViews()
                adRoot.visibility = View.VISIBLE
                mTTSplashAd?.showAd(adRoot)
            } else {
                LogC.e("开屏 -> showAd...广告无效...")
                listener?.onAdLoadFail(AdConstants.AD_ERROR_CODE_NOT_LOAD_SUCCESS)
                release()
            }

        } catch (e: java.lang.Exception) {
            e.printStackTrace()
            listener?.onAdLoadFail(AdConstants.AD_ERROR_CODE_EXCEPTION)
            release()
        }
    }

    override fun release() {
        super.release()
        mTTSplashAd?.destroy()
        mTTSplashAd = null
    }
}